<?php

namespace App\Models\OpenShop\InvestmentPromotion;

use App\Enums\OpenShop\InvestmentPromotion\UserIdentityCardEnum;
use App\Models\BaseModel;
use App\Service\OpenShop\Feishu\MainProcessService;
use App\Support\ChangeSupport;

class OpenShopUserIdentityCardModel extends BaseModel
{
    protected $table = 'open_shop_user_identity_card';

    protected $fillable = [
        'orderNo',
        "frontSideImg",
        "backSideImg",
        "name",
        "identityCardNumber",
        "gender",
        "effectiveStartTime",
        "effectiveEndTime",
        "nativePlace",
        "nation",
        "birthday",
        "animalSign",
        "constellation",
        "createdAt",
        "createdByUid",
        "updatedAt",
        "updatedByUid"
    ];

    public static array $orderLogTranslate = [
        "frontSideImg"       => [
            "type" => ChangeSupport::FILED_TYPE_PRIVATE_PICTURE,
            "desc" => "身份证正面图"
        ],
        "backSideImg"        => [
            "type" => ChangeSupport::FILED_TYPE_PRIVATE_PICTURE,
            "desc" => "身份证背面图"
        ],
        "name"               => "姓名",
        "identityCardNumber" => "身份证号码",
        "gender"             => [
            "type" => ChangeSupport::FILED_TYPE_ENUM,
            "desc" => "性别"
        ],
        "effectiveStartTime" => [
            "type" => ChangeSupport::FILED_TYPE_DATE_YMD,
            "desc" => "身份证有效期开始时间"
        ],
        "effectiveEndTime"   => [
            "type" => ChangeSupport::FILED_TYPE_DATE_YMD,
            "desc" => "身份证有效期截止时间"
        ],
        "nativePlace"        => "籍贯",
        "nation"             => "民族",
        "birthday"           => "生日",
        "animalSign"         => "生肖",
        "constellation"      => "星座"
    ];

    public static function getDataByOrderNo(string $orderNo)
    {
        return self::query()->where('orderNo', $orderNo)->first();
    }

    public function mainProcessRel()
    {
        return $this->hasMany(OpenShopMainProcessModel::class, 'orderNo', 'orderNo');
    }

    // 通过身份证校验性别
    public static function getCardSex(string $identityNo): int
    {
        $sexint = (int)substr($identityNo, 16, 1);
        if (!is_numeric($sexint)) {
            return UserIdentityCardEnum::GENDER_2;
        }

        return $sexint % 2 === 0 ? UserIdentityCardEnum::GENDER_1 : UserIdentityCardEnum::GENDER_2;
    }

    public function getSexTextAttribute()
    {
        $sexInt = self::getCardSex($this->identityCardNumber);

        return $sexInt == UserIdentityCardEnum::GENDER_1 ? '女' : '男';
    }

    /**
     * 通过身份证获取年龄和性别
     * @param string $identityNo
     * @return array
     */
    public static function getCardAgeSex(string $identityNo): array
    {
        if (strlen($identityNo) != 18) {
            return [0, ''];
        }

        $pattern = '/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})(\d|X)$/';
        preg_match($pattern, $identityNo, $matches);

        $year = $matches[2];
        $month = $matches[3];
        $day = $matches[4];

        $currentYear = date('Y');
        $currentMonth = date('m');
        $currentDay = date('d');

        $age = $currentYear - $year;

        if ($currentMonth < $month || ($currentMonth == $month && $currentDay < $day)) {
            $age--;
        }

        $genderDigit = (int)substr($identityNo, -2, 1);
        $gender = $genderDigit % 2 === 0 ? '女' : '男';

        return [$age, $gender];
    }
}
